import sdk from '../core/index';
import { DataEquip } from "../shared/face";
import type { Skill } from "../core/rpg/types";
import SkManager from "../core/rpg/skill/SkManager";
import { analysisIntent } from "../core/tool/baseScene";
import { mathType } from "../core/tool/face";
import common from "../core/tool/common";
import component, { ComType } from "../core/tool/component";
import { Icard } from "../core/tool/Icard";
import bag_component from "./bag_component";
import rpg_component from "./rpg_component";
import { Icard2 } from "../core/tool/Icard2";
import { EquipService } from "../service/EquipService";

/**
 * 装备组件 - 现代化组件，使用依赖注入
 * 职责：
 * 1. 管理装备栏状态
 * 2. 处理用户交互
 * 3. 显示装备信息
 * 4. 通过注入的EquipService处理业务逻辑
 */
export default class equip_component extends component {
    private _equipService: EquipService;
    slot: DataEquip[] = [];
    
    constructor() {
        super();
        // 使用依赖注入获取EquipService（_开头，不会被持久化）
        this._equipService = this.getService<EquipService>('EquipService');
    }

    setup_default() {
        this.slot = [];
    }

    /**
     * 获取装备栏总属性值
     * @param key 属性键
     * @returns 属性值
     */
    getAttribute(key: string): number {
        return this._equipService.getTotalAttribute(this.slot, key);
    }
    /**
     * 装备栏主界面
     */
    client_main() {
        if (this.slot.length == 0) {
            this.client_text('装备栏空空如也哦~');
            return;
        }
        
        const card = this.createEquipListCard();
        this.client_text(card.getText());
        return card;
    }

    /**
     * 创建装备列表卡片
     */
    private createEquipListCard(): Icard {
        const card = sdk.tool.card.create();
        card.title('我的装备');
        
        this.slot.forEach((equip, index) => {
            const displayName = this._equipService.getDisplayName(equip);
            card.l(`[${index + 1}]${equip.emoji} ${displayName}`);
        });
        
        this.registerEquipButtonIntents(card);
        return card;
    }
    client_look(data:analysisIntent){
        let index = sdk.tool.common.cover_number(data.Message.content);
        if(!index.k1){
            data.player.client_text('请输入装备栏编号');
            return;
        }
        let equip = this.slot[index.k1 - 1];
        if(!equip){
            data.player.client_text('装备不存在');
            return;
        }
        let card = sdk.tool.card.create2();
        sdk.prop.client_equip(equip,card);
        data.player.client_img('panel_common',card.resData);
    }
    /**
     * 卸下装备
     */
    client_del(data: analysisIntent) {
        const index = sdk.tool.common.cover_number(data.Message.content);
        if (!index.k1) {
            data.player.client_text('请输入装备栏编号');
            return;
        }

        const equipIndex = index.k1 - 1;
        const result = this._equipService.unequipItem(equipIndex, this.slot);
        
        if (!result.success) {
            data.player.client_text(result.message);
            return;
        }

        // 更新装备栏
        this.slot = result.newEquipList;
        
        // 将卸下的装备放入背包
        if (result.unequippedEquip) {
            const item = this._equipService.equipToItem(result.unequippedEquip);
            const bag_com = this.getComponent(ComType.背包) as bag_component;
            bag_com.addItem(item);
        }
        
        data.player.client_text(result.message);
    }
    /**
     * 添加装备（穿戴）
     */
    addEquip(equip: DataEquip) {
        const result = this._equipService.equipItem(equip, this.slot);
        
        if (!result.success) {
            this.client_text(result.message);
            return;
        }

        // 更新装备栏
        this.slot = result.newEquipList;
        
        // 如果有被替换的装备，放入背包
        if (result.replacedEquip) {
            const item = this._equipService.equipToItem(result.replacedEquip);
            const bag_com = this.getComponent(ComType.背包) as bag_component;
            bag_com.addItem(item);
        }
        
        this.client_text(result.message);
        
        // 更新战力显示
        const rpg_com = this.getComponent(ComType.rpg) as rpg_component;
        rpg_com.reload_skills();
        rpg_com._clientHandler.showPowerChange();
    }
    // 创意 - 当敌人的攻击过于强大时，有概率击碎自身装备
    // 主武器
    // 副武器
    // 上身
    // 下身
    // 鞋子

    // 注册装备按钮意图的通用方法
    private registerEquipButtonIntents(card: Icard): void {
        const buttonConfigs = [
            { text: '查看', action: 'client_look', mathType: mathType.开头匹配 },
            { text: '卸下', action: 'client_del', mathType: mathType.开头匹配 }
        ];
        
        this.registerButtonIntents(card, buttonConfigs);
    }

    /**
     * 获取装备栏状态信息
     */
    getEquipStatus(): {
        equipCount: number;
        totalAttributes: { [key: string]: number };
        equipList: DataEquip[];
    } {
        const totalAttributes: { [key: string]: number } = {};
        
        // 计算常用属性
        const commonAttributes = ['物理攻击', '魔法攻击', '物理防御', '魔法防御', '生命值', '法力值'];
        commonAttributes.forEach(attr => {
            totalAttributes[attr] = this.getAttribute(attr);
        });
        
        return {
            equipCount: this.slot.length,
            totalAttributes,
            equipList: [...this.slot]
        };
    }

    /**
     * 检查是否有特定类型的装备
     */
    hasEquipType(equipType: string): boolean {
        return this.slot.some(equip => equip.solt_type === equipType);
    }

    /**
     * 获取特定类型的装备
     */
    getEquipByType(equipType: string): DataEquip | null {
        return this.slot.find(equip => equip.solt_type === equipType) || null;
    }

    /**
     * 清空装备栏
     */
    clearAllEquips(): void {
        const bag_com = this.getComponent(ComType.背包) as bag_component;
        
        // 将所有装备放入背包
        this.slot.forEach(equip => {
            const item = this._equipService.equipToItem(equip);
            bag_com.addItem(item);
        });
        
        this.slot = [];
        this.client_text('已将所有装备放入背包');
    }
}